var errorImgHtml = '<img src="../../staticFile/images/pic12.gif" width="16" height="16"/>';
$(function(){
	setInputWidth();
	$('#loginName').blur(function(){
		if (isNull(V('loginName'))){
			showNameErrorMsg('请输入登录名');
		}
	});
	$('#loginName').focus(function(){
		$('#nameErrorMsg').hide();
		$('#loginName').css({'border':''});
	});
	$('#btnNextWriteName').click(function(){
		if (isNull(V('loginName'))){
			showNameErrorMsg('请输入登录名');
			return;
		}
		this.disabled = true;
		verifyUserInfo(this);
	});
	$('#btnSendCheckCode').click(function () {
		var time = 60;//60秒倒计时
		var phoneObj = this;
		phoneObj.disabled = true;
		var checkCodeInterval = window.setInterval(function () {
			time --;
			if (time < 1) {
				window.clearInterval(checkCodeInterval);
				$(phoneObj).text('发送验证码');
				//$('#phoneErrorMsg').text('');
				phoneObj.disabled = false;
			}else {
				$(phoneObj).text(time + '秒后可点击');
			}
		},1000);
		//获取验证码
		goSendCheckCode();
	});
	$('#modifyPwdCheckCode').focus(function () {
		$('#phoneVerifyErrorMsg').hide();
	});
	$('#newPwd,#sureNewPwd').focus(function () {
		$('#pwdErrorMsg').hide();
	});
	$('#btnSureModify').click(function() {
		this.disabled = true;
		sureModifyPwd(this);
	});
});
function showNameErrorMsg(errorMsg){
	$('#nameErrorMsg').show().html(errorImgHtml + errorMsg);
	$('#loginName').css('border','1px #ff6749 solid');
}
/**
 * 获取用户相关信息
 * @returns
 */
function verifyUserInfo(btnObj) {
	//从后台判断用户名是否存在
	var fpLoginName = V('loginName');
	$.ajax({
		type : "POST",
		url : '/index/user/queryUserInfo',
		data : {USERNAME : fpLoginName},
		dataType : 'json',
		async: false,
		success : function(result) {
			btnObj.disabled = false;
			if ("0" == result.status) {
				$('#verifyLoginPhone').text(result.PHONE);
				$('#verifyName').hide();
				$('#verifyByCode').show();
			} else {
				showNameErrorMsg('用户名不存在或已被冻结');
			}
		},
		error: function() {
			btnObj.disabled = false;
			showNameErrorMsg('系统繁忙，请稍候再试');
		}
	});
}
//获取验证码
function goSendCheckCode() {
	var pvErr = $('#phoneVerifyErrorMsg');
	pvErr.show();
	$.ajax({
		type: "POST",
		url: '/index/user/sendPhoneCheckCode',
		data:{USERNAME: V('loginName'),isByDb: 'Y'},
		dataType: 'json',
		async: false,
		success: function(result) {
			if(result.state=='1'){
				pvErr.html('验证码已发送');
			}else if(result.state=='4'){
				pvErr.html(errorImgHtml + '您当前IP地址获取验证码次数过多，请稍候再试');
			}else if(result.state=='5'){
				pvErr.html(errorImgHtml + '您当前手机号码获取验证码次数过多，请稍候再试');
			}else {
				pvErr.html(errorImgHtml + '当前系统繁忙,请稍后再获取验证码');
			}
		},
		error: function() {
			pvErr.html(errorImgHtml + '当前系统繁忙,请稍后再获取验证码');
		}
	});
}
/**
 * 确认修改密码
 * @returns
 */
function sureModifyPwd(btnSureObj) {
	var modifyPwdCheckCode = V('modifyPwdCheckCode'),newPwd = V('newPwd'), sureNewPwd = V('sureNewPwd');
	if (isNull(modifyPwdCheckCode)) {
		showPwdErrorMsg('验证码不能为空');
		btnSureObj.disabled = false;
		return;
	}
	if (isNull(newPwd)) {
		showPwdErrorMsg('新密码不能为空');
		btnSureObj.disabled = false;
		return;
	}
	if (isNull(sureNewPwd)) {
		showPwdErrorMsg('确认新密码不能为空');
		btnSureObj.disabled = false;
		return;
	}
	if (newPwd != sureNewPwd) {
		showPwdErrorMsg('新密码和确认密码不相等');
		btnSureObj.disabled = false;
		return;
	}
	var shaObj = new jsSHA("SHA-1", "TEXT");
	shaObj.update(newPwd);
	newPwd = shaObj.getHash("HEX");
	popTipShow.confirm('修改', '您确定要修改密码吗？', ['确定', '取消'], function (e) {
		this.hide();
		if ($(e.target).attr('class') != 'ok') {
			btnSureObj.disabled = false;
			return;
		}
		$.ajax({
			type: "POST",
			url: '/index/user/modifyPwdByCheckCode',
			data:{USERNAME: V('loginName'),checkCode: V('modifyPwdCheckCode'),newPwd: newPwd},
			dataType: 'json',
			async: false,
			success: function(result) {
				if(result.status=='1'){
					showPwdErrorMsg('修改密码时出错');
				}else if(result.status=='3'){
					showPwdErrorMsg('未获取到用户名');
				}else if(result.status=='4'){
					showPwdErrorMsg('未获取到用户密码');
				}else if(result.status=='5'){
					$('#phoneVerifyErrorMsg').show().html('验证码错误或已失效');
				}else if(result.status=='0'){
					showPwdErrorMsg('密码修改成功,快去登录吧');
					$('#goLogin').show();
					$('#verifyByCode').hide();
				}else {
					showPwdErrorMsg('密码修改失败');
				}
				btnSureObj.disabled = false;
			},
			error: function() {
				showPwdErrorMsg('当前系统繁忙,请稍后再获取验证码');
				btnSureObj.disabled = false;
			}
		});
	});
}
function showPwdErrorMsg(errorMsg){
	$('#pwdErrorMsg').show().html(errorImgHtml + errorMsg);
}
function setInputWidth() {
	var rem = $('html').css('font-size').replace('px',''), screenWidth = $(document).width();
	//0.6*2+0.2*2
	$('.verify-name input').css('width', (screenWidth - 4 - parseInt(rem) * 1.6) + 'px');
}